package com.xm.xmapp;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.adapter.Charging_pile_listAdapter;
import com.xm.xmapp.adapter.Charging_pile_listAdapter.PileOnclickInterFace;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.XListView;
import com.xm.xmapp.customview.XListView.IXListViewListener;
import com.xm.xmapp.dialog.PilePasswordDialog;
import com.xm.xmapp.dialog.PilePasswordDialog.DissMissable;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Charging_pile_listRequest;
import com.xm.xmapp.request.bean.Start_chargeRequest;
import com.xm.xmapp.response.bean.ResponseChargingPileList;
import com.xm.xmapp.response.bean.ResponseChargingPileList.E;
import com.xm.xmapp.response.bean.ResponseChargingPileList.T;
import com.xm.xmapp.response.bean.ResponseStartCharge;

public class PileSelectActivity extends BaseActivity {

	private int id;
	private Charging_pile_listAdapter adapter;
	private ArrayList<ResponseChargingPileList.E> list;
	private ResponseChargingPileList.T bean;
	private XListView xListView;
	Charging_pile_listRequest requestBean;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_pile);
	}

	@Override
	public void initView() {
		xListView = (XListView) findViewById(R.id.xListView);
	}

	@Override
	public void initData() {

		id = getIntent().getIntExtra("id", 0);

		setMyTitle("选桩充电");
		list = new ArrayList<ResponseChargingPileList.E>();
		bean = new T();
		bean.available = new ArrayList<ResponseChargingPileList.E>();
		bean.disabled = new ArrayList<ResponseChargingPileList.E>();
		adapter = new Charging_pile_listAdapter(list, this, R.layout.item_pile);
		adapter.setPileOnclickInterFace(new PileOnclickInterFace() {

			@Override
			public void start(final E t) {

				DissMissable mDissMissable = new DissMissable() {

					@Override
					public void getPsw(String pwd) {
						Start_chargeRequest requestBean = new Start_chargeRequest();
						requestBean.chargeType = 2;
						requestBean.connectorId = t.charging_equipment_interface_id;
						requestBean.userPin = pwd;
						promptDialog.show();
						GetDataInterFace<String> listener = new GetDataInterFace<String>() {

							@Override
							public void onResponse(String response) {
								promptDialog.dismiss();
								ResponseStartCharge start_chargeBean = JSON.parseObject(response,
										ResponseStartCharge.class);
								MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class,
										Type.A_Main.START_CHANG, start_chargeBean);
								finish();

							}
						};
						MyAppcation.getMyAppcation().getPostData(PileSelectActivity.this, requestBean, listener,
								new DisssmissInterFace() {

									@Override
									public void putError(VolleyError error) {
										promptDialog.dismiss();
									}
								});
					}

				};

				PilePasswordDialog passwordDialog = new PilePasswordDialog(PileSelectActivity.this, mDissMissable);
				passwordDialog.show();

			}

			@Override
			public void item(E t) {

				intent = new Intent(MyAppcation.getMyAppcation(), PileDetailsActivity.class);
				intent.putExtra("bean", t);
				startActivity(intent);
			}
		});

		xListView.setAdapter(adapter);
		xListView.setPullRefreshEnable(true);
		xListView.setPullLoadEnable(true);
		getData();
	}

	private void getData() {

		if (requestBean == null) {
			requestBean = new Charging_pile_listRequest();
			requestBean.charging_pile_network_id = id;
			requestBean.pagesize = CodeConstant.pageSize;
			requestBean.page = 1;
		}

		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				xListView.stopRefresh();
				xListView.stopLoadMore();
				promptDialog.dismiss();

				ResponseChargingPileList base = JSON.parseObject(response, ResponseChargingPileList.class);

				if (requestBean.page == 1) {

					bean.available.clear();
					bean.disabled.clear();
				}
				list.clear();
				bean.available.addAll(base.list.available);
				bean.disabled.addAll(base.list.disabled);
				list.addAll(bean.available);
				list.addAll(bean.disabled);
				adapter.notifyDataSetChanged();

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
				xListView.stopRefresh();
				xListView.stopLoadMore();
			}
		});

	}

	@Override
	public void initEvent() {
		xListView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				requestBean.page = 1;
				getData();

			}

			@Override
			public void onLoadMore() {
				requestBean.page++;
				getData();
			}
		});
	}
}
